package com.nlx.notes.module.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nlx.notes.module.bean.dto.category.CategoryUpdateDTO;
import com.nlx.notes.module.entity.UserCategoryModification;
import com.nlx.notes.module.mapper.UserCategoryModificationMapper;
import com.nlx.notes.module.service.IUserCategoryModificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nlx
 * @since 2025-11-16
 */
@Slf4j
@Service
public class UserCategoryModificationServiceImpl extends ServiceImpl<UserCategoryModificationMapper, UserCategoryModification> implements IUserCategoryModificationService {

    @Override
    public UserCategoryModification findByCateGory(Long categoryId, Long userId) {
        if(null == categoryId || null == userId){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(UserCategoryModification.class)
                .eq(UserCategoryModification::getCategoryId,categoryId)
                .eq(UserCategoryModification::getUserId,userId),false);
    }

    @Override
    public void saveCategoryModification(CategoryUpdateDTO categoryUpdateDTO) {
        UserCategoryModification userCategoryModification = new UserCategoryModification();
        userCategoryModification.setUserId(categoryUpdateDTO.getUserId());
        userCategoryModification.setCategoryId(categoryUpdateDTO.getId());
        userCategoryModification.setName(categoryUpdateDTO.getCategoryName());
        userCategoryModification.setIsDeleted(0);
        userCategoryModification.setModificationType("updated");
        this.save(userCategoryModification);
    }

    @Override
    public boolean updateCategoryModification(CategoryUpdateDTO categoryUpdateDTO) {
        UserCategoryModification userCategoryModification = new UserCategoryModification();
        UserCategoryModification cateGory = this.findByCateGory(categoryUpdateDTO.getId(), categoryUpdateDTO.getUserId());
        userCategoryModification.setUserId(categoryUpdateDTO.getUserId());
        userCategoryModification.setCategoryId(categoryUpdateDTO.getId());
        userCategoryModification.setName(categoryUpdateDTO.getCategoryName());
        userCategoryModification.setCategoryIconId(categoryUpdateDTO.getCategoryIconId());
        if("delete".equals(categoryUpdateDTO.getOperationType())){
            userCategoryModification.setIsDeleted(1);
            userCategoryModification.setModificationType("delete");
        }else{
            userCategoryModification.setIsDeleted(0);
            userCategoryModification.setModificationType("modified");
        }
        if(cateGory != null){
            userCategoryModification.setId(cateGory.getId());
            return this.updateById(userCategoryModification);
        }else{
            return this.save(userCategoryModification);
        }
    }
}
